<template>
  <div class="forget_pwd">
    <headTop :headClass="'transparent_header'">Forget the password</headTop>
    <div class="input_info">
      <label for="phone" class="info">
        <i class="iconfont icon-msnui-tel"></i>
        <input type="number" id="phone" placeholder="Please enter your phone number" @input="checkPhone" v-model="user.phone">
      </label>
      <div class="info send_code">
        <label for="verify" class="verify">
          <i class="iconfont icon-yanzhengmatianchong"></i>
          <input type="number" id="verify" placeholder="Please enter SMS code" v-model="user.verify">
        </label>
        <span :class="{'no_send' : !isSend, 'is_send' : isSend}" @click="sendCode">{{sendText}}</span>
      </div>
      <label for="password" class="info">
        <i class="iconfont icon-mima"></i>
        <input type="password" id="password" placeholder="Please set up a new login password" v-model="user.password">
      </label>
      <label for="repassword" class="info">
        <i class="iconfont icon-mima"></i>
        <input type="password" id="repassword" placeholder="Please confirm the password again" v-model="user.rePassword">
      </label>
    </div>
    <div class="confirm">
      <span class="public_btn" @click="submit">Sure</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'forgetPassword',
  data () {
    return {
      user: {
        phone: '',
        verify: '',
        password: '',
        rePassword: ''
      },
      sendText: 'Get code',
      isSend: false,
      codeTime: 60
    };
  },
  methods: {
    // Send verification code
    sendCode () {
      if (!this.user.phone) {
        this.Toast('Please enter cell phone number.');
      } else {
        if (this.isSend) {
          return false;
        } else {
          this.getCode();
        }
      }
    },
    // Count down
    timer () {
      if (this.codeTime > 0) {
        this.codeTime--;
        this.sendText = `(${this.codeTime})s`;
        setTimeout(this.timer, 1000);
      } else {
        this.codeTime = 60;
        this.sendText = 'Recapture';
        this.isSend = false;
      }
    },
    // Verify cell phone number
    checkPhone () {
      if (!/^(1[3-9])\d{9}$/.test(this.user.phone)) {
        this.isSend = true;
      } else {
        this.isSend = false;
      }
    },
    // Submission of registration
    submit () {
      if (!this.user.phone) {
        this.Toast('Please enter cell phone number.');
      } else if (!this.user.verify) {
        this.Toast('Please input verification code.');
      } else if (!this.user.password) {
        this.Toast('Please enter a new login password.');
      } else if (this.user.password.length < 6) {
        this.Toast('Please enter6-16Login password');
      } else if (!this.user.rePassword) {
        this.Toast('Please enter confirmation password.');
      } else if (this.user.password !== this.user.rePassword) {
        this.Toast('Two password inconsistency');
      } else {
        this.goRequest();
      }
    },
    // Forget the password
    goRequest () {
      this.axios.post('index/auth/loinPass', {
        phone: this.user.phone,
        code: this.user.verify,
        pass: this.user.password,
        repass: this.user.rePassword
      }).then(({data}) => {
        if (data.status === 200) {
          this.Toast('Set up success, Please log in');
          this.$router.go(-1);
        } else {
          this.Toast(data.message);
        }
      });
    },
    // Sending verification code request
    getCode () {
      this.axios.post('index/sms/send', {
        phone: this.user.phone,
        type: 2
      }).then(({data}) => {
        if (data.status === 200) {
          this.isSend = true;
          this.timer();
        } else {
          this.Toast(data.message);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
  @import '../style/mixin.scss';
  .forget_pwd{
    padding-top: 160px;
    background-color: #f5f5f5;
    .input_info{
      width: 100%;
      padding: 0 75px 100px;
      .info{
        width: 100%;
        height: 88px;
        border-radius: 44px;
        display: flex;
        align-items: center;
        padding: 0 40px;
        background-color: #fff;
        border: 1px solid #e6e6e6;
        margin-bottom: 40px;
        .iconfont{
          font-size: 36px;
          color: $color;
          margin-right: 30px;
        }
        input{
          flex: 1;
          height: 100%;
          font-size: 28px;
          color: #282828;
          &:placeholder{
            color: #999;
          }
        }
        &.send_code{
          .verify{
            width: 70%;
            height: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            input{
              flex: 1;
              height: 100%;
            }
          }
          >span{
            width: 30%;
            font-size: 28px;
            color: $color;
            text-align: right;
            &.is_send{
              color: #666;
            }
          }
        }
        &:nth-last-child(1){
          margin-bottom: 0;
        }
      }
      .memory{
        width: 100%;
        padding: 0 36px;
        font-size: 26px;
        color: #666;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left{
          .iconfont{
            font-size: 30px;
            color: $color;
            margin-right: 14px;
          }
        }
      }
    }
    .confirm{
      width: 100%;
      text-align: center;
    }
  }
</style>
